/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package web.service.Impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import web.util.DBUitl;
import web.Dao.impl.UserDAOImpl;
import web.enity.UserInfo;
import web.service.UserService;
import web.Dao.UserDAO;
import web.exception.AccountNotFoundException;
import web.exception.IncorrectPasswordException;

/**
 *
 * @author 10592
 */
public class UserServiceImpl implements UserService {

    public UserServiceImpl() {
        super();
    }
  
    @Override
    public UserInfo Login(UserInfo user) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.;;;
        int r=0;
        UserDAO UserBiz = new UserDAOImpl();
        //查找账户是否存在
        System.out.println(UserBiz.quary(user.getUserName()));
        if (UserBiz.quary(user.getUserName())==false) {
           throw new AccountNotFoundException();
        }
        //登录
        UserInfo ui=UserBiz.login(user.getUserName(), user.getPassword());
        if (ui==null) {
            throw new IncorrectPasswordException();
        }
       
        return  ui;
    }

    @Override
    public int Resiger(UserInfo userInfo) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.?
        int r = 0;
        UserDAO UserBiz = new UserDAOImpl();
        if (UserBiz.quary(userInfo.getUserName())) {
            System.err.println("用户名已存在，请重新输入");
        }
        else{
            r=UserBiz.resiger(userInfo);
            if (r==0) {
                System.out.println("注册失败，请重试");
            }
            else{
                System.out.println("注册成功");
            }
        }
        return r;
    }
  
    public static void main(String[] args) {
        UserService us=new UserServiceImpl();
        UserInfo ui=new UserInfo("111", "111");
        System.out.println(us.getAll("555"));
    }

    @Override
    public int getAll(String userName) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        UserDAO UserBiz = new UserDAOImpl();
        if (UserBiz.quary(userName)== false) {
           return 0;//用户存在
        }
        else
            return 1;
    }
}
